﻿using UnityEngine;
using System.Collections;
using UnityEngine.UI;

/* Comment
 * 截取用户的输入，判断输入的案件信息应该交给那个模块处理
 * 当Square被选定时候，所有信息交给移动处理
 * 当摄像机被选定时候，所有信息交给信息处理
 * UI选定时候，所有信息交给UI处理
 */

public enum ESelTarget
{
	emSelNone,
	emSelCam,
	emSelSquare,
	emSelUI,
}

public class EventMouseInput : System.EventArgs
{
	public ESelTarget eTarget;
	public GameObject dstObj;

	public EventMouseInput(ESelTarget tgt, GameObject obj)
	{
		eTarget = tgt;
		dstObj = obj;
	}
}

public class UserInputManager : MonoBehaviour {

	public RectTransform UIzone;
	public Canvas cv;
	public CanvasScaler cvslr;

	public event System.EventHandler<EventMouseInput> evthMouseInput;

	ESelTarget eTarget;
	GameObject targetObj;


	// Use this for initialization
	void Start () {
		eTarget = ESelTarget.emSelNone;
	}

	// test if it is a UI hit
	bool UIHitTest()
	{
		float UIScaleFactor = cv.pixelRect.width / cvslr.referenceResolution.x;

		/*Debug.Log("intpumousePosition" + Input.mousePosition + "cv x:" + cv.pixelRect.x
		          + "cv width:" + cv.pixelRect.width + "UIZone y min:" + UIzone.offsetMax.x + "UIZone y max:" + UIzone.offsetMax.y * UIScaleFactor);*/

		if (Input.mousePosition.x > cv.pixelRect.x && Input.mousePosition.x < cv.pixelRect.width)
		{
			if(Input.mousePosition.y > UIzone.offsetMax.x && Input.mousePosition.y < UIzone.offsetMax.y * UIScaleFactor)
			{
				return true;
			}
		}

		return false;
	}

	// Update is called once per frame
	void Update () {
		switch (eTarget)
		{
		case ESelTarget.emSelNone:
			// 有按键按下
			if(Input.GetMouseButtonDown(0))
			{
				if (UIHitTest())
				{
					eTarget = ESelTarget.emSelUI;
						break;
				}

				Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);

				RaycastHit hit;

				LayerMask layerMaskUnit = 1 << LayerMask.NameToLayer("Units"); 
				if (Physics.Raycast(ray, out hit, 1000, layerMaskUnit))
				{
					eTarget = ESelTarget.emSelSquare;
					targetObj = hit.collider.gameObject;
					
					evthMouseInput(this, new EventMouseInput(eTarget, hit.collider.gameObject));
					// jump out switch ，select over
					break; 
				}

				LayerMask layerMaskGround = 1 << LayerMask.NameToLayer("Ground"); 

				if (Physics.Raycast(ray, out hit, 1000, layerMaskGround))
				{
					eTarget = ESelTarget.emSelCam;
					targetObj = hit.collider.gameObject;
					
					evthMouseInput(this, new EventMouseInput(eTarget, targetObj));
					// sendmessage and jump out
					break;
				}



				/*RaycastHit[] hits = Physics.RaycastAll(ray);

				// 直接选中地面才认为是要操作摄像机
				if (hits.Length > 0)
				{
					// 操作摄像机
					if(hits[0].collider.gameObject.tag == "Ground")
					{
						eTarget = ESelTarget.emSelCam;
						targetObj = hits[0].collider.gameObject;

						evthMouseInput(this, new EventMouseInput(eTarget, targetObj));
						// 发送信息，并跳出
						break;
					}
				}

				// 检查是否选中部队
				foreach (RaycastHit hit in hits)
				{
					if(hit.collider.gameObject.tag == "Square")
					{
						eTarget = ESelTarget.emSelSquare;
						targetObj = hit.collider.gameObject;

						evthMouseInput(this, new EventMouseInput(eTarget, hit.collider.gameObject));
						// 跳出 switch ，选择结束
						break; 
					}
				}*/
			}

			break;
		case ESelTarget.emSelCam:
		case ESelTarget.emSelSquare:

			// 直接转发，所有信息
			evthMouseInput(this, new EventMouseInput(eTarget, targetObj));
			// 如果取消选择:
			if (Input.GetMouseButtonUp(0))
			{
				eTarget = ESelTarget.emSelNone;
				targetObj = null;
			}
			break;
		case ESelTarget.emSelUI:

			if (Input.GetMouseButtonUp(0))
			{
				eTarget = ESelTarget.emSelNone;
				targetObj = null;
			}
			break;
		default:
			break;
		}


	}
}
